/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.txweb.view;

import com.jspx.txweb.OAuthClient;
import com.jspx.txweb.annotation.Safety;
import com.jspx.txweb.support.ActionSupport;
import com.jspx.txweb.util.TXWebUtil;
import com.jspx.utils.StringUtil;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: yuan
 * Date: 12-12-15
 * Time: 下午2:04
 * 认证挂接接口
 */
public abstract class AuthenticationView extends ActionSupport {


    //登录ID
    protected String loginId = StringUtil.empty;

    //是否密码已经加密
    protected int passwordEncoded = 0;

    //登录密码
    protected String password;
    //默认为用户名登录
    protected String field = "name";

    protected long timeMillis = System.currentTimeMillis();

    protected String privateKey;

    protected String publicKey;

    @Safety(length = 80)
    public void setField(String field) {
        this.field = field;
    }





    @Safety(length = 200)
    public void setLoginId(String loginId) {
        this.loginId = loginId;
    }

    @Safety(length = 80)
    public void setPassword(String password) {
        this.password = password;
    }

    @Safety
    public void setPasswordEncoded(int passwordEncoded) {
        this.passwordEncoded = passwordEncoded;
    }


    public void setTimeMillis(long timeMillis) {
        this.timeMillis = timeMillis;
    }


    public void setPrivateKey(String privateKey) {
        this.privateKey = privateKey;
    }

    @Safety(max = 300)
    public void setPublicKey(String publicKey) {
        this.publicKey = publicKey;
    }

    public String getMakeVerify()
    {
        return  TXWebUtil.getLoginHashVerify(field, loginId, publicKey, privateKey, password, passwordEncoded, timeMillis);
    }

    public Map<String,Object> createPostParameter()
    {
        Map<String,Object> postParameter = new HashMap<String, Object>();
        postParameter.put(OAuthClient.KEY_loginField, field);
        postParameter.put(OAuthClient.KEY_loginId, loginId);
        postParameter.put(OAuthClient.KEY_timeMillis,Long.toString(timeMillis));
        postParameter.put(OAuthClient.KEY_verify,getMakeVerify());
        postParameter.put(OAuthClient.KEY_password,password);
        postParameter.put(OAuthClient.KEY_passwordEncoded,Integer.toString(passwordEncoded) );
        return postParameter;
    }

    public void debug() {
        System.out.println("[" + field + ":" + loginId + "];" + publicKey + privateKey + StringUtil.SEMICOLON + password + StringUtil.SEMICOLON + passwordEncoded + StringUtil.SEMICOLON + timeMillis);
    }

}